<?php
/**
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/10
 * Time: 10:52
 */

namespace app\api\business;


use app\api\controller\UserCommon;
use app\api\model\Cash;
use app\api\model\User;
use think\Exception;
use think\facade\Db;

class CashBus extends UserCommon
{
    public $model;
    public function __construct(){
        parent::__construct();
        $this->model = new Cash();
    }


    public function todo($param){
        $money = (new User())->where('user_id',$this->user_id)->lock(true)->value('money');
        if ($money < $param['amount']){
            throw new Exception('余额不足');
        }
        $data = [
            'amount' => $param['amount'],
            'user_id' => $this->user_id,
            'type' => $param['type'],
            'service_price' => $param['amount'] * 0.1,
            'integral' => $param['amount'] * 0.1,
            'account_name' => $param['account_name'],
            'account' => $param['account'],
            'create_time' => date('Y-m-d H:i:s'),
            'date' => date('Y/m/d'),
        ];
        if ($param['type'] == 2){
            $data['bank_name'] = $param['bank_name'];
        }
        // 插入提现记录
        $this->model->insert($data);
        // 减扣余额
        Db::name('user')->where('user_id',$this->user_id)->dec('money',$param['amount'])->update();
    }

    public function getList($status){
        $where = [
            'user_id' => $this->user_id
        ];
        if ($status != 0){
            $where['status'] = $status;
        }
        $orders = Db::name('cash')->Where($where)->select()->toArray();
        return $orders;
    }

}